package com.xyz.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.xyz.gulimall.product.entity.AttrEntity;
import com.xyz.gulimall.product.service.AttrAttrgroupRelationService;
import com.xyz.gulimall.product.service.AttrService;
import com.xyz.gulimall.product.service.CategoryService;
import com.xyz.gulimall.product.vo.AttrGroupAttrsVo;
import com.xyz.gulimall.product.vo.AttrGroupRealtionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xyz.gulimall.product.entity.AttrGroupEntity;
import com.xyz.gulimall.product.service.AttrGroupService;
import com.xyz.common.utils.PageUtils;
import com.xyz.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author xiao
 * @email 2382414125@qq.com
 * @date 2021-02-22 15:52:13
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId")Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }

    @Resource
    private AttrGroupService attrGroupService;

    @Resource
    private CategoryService categoryService;

    @Resource
    AttrService attrService;

    @Resource
    AttrAttrgroupRelationService relationService;

    /**
     * /product/attrgroup/{catelogId}/withattr
     */
    @GetMapping("/{catelogId}/withattr")
    public R fandWithattr(@PathVariable("catelogId")Long catId){
        System.err.println("catId:"+catId);
        List<AttrGroupAttrsVo> attrGroupAttrsVos = attrGroupService.fandWithattr(catId);
        return R.ok().put("data",attrGroupAttrsVos);
    }

    /**
     * /product/attrgroup/1/attr/relation
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }

    /**
     * /product/attrgroup/{attrgroupId}/noattr/relation
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        //page.getList().forEach(System.err::println);
        return R.ok().put("page",page);
    }

    ///product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRealtionVo> vos){

        relationService.saveBatch(vos);
        return R.ok();
    }
    /**
     * 删除 /product/attrgroup/attr/relation/delete
     */
    @PostMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody AttrGroupRealtionVo[] realtionVo){
        attrService.deleteRelation(realtionVo);
        return R.ok();
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        //找到catelogId的完整路径[父/子...]
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
